<?php
define(dbhost,"localhost");
define(dbusername,"root");
define(dbpassword,"1491");
define(dbname,"Mkart");


class prod_list{
	public function getdata($dataobject){
		$db = new mysqli(dbhost,dbusername,dbpassword,dbname);
		$db->query("SET NAMES utf8");
		
		$sql = "select * from product order by id " ;
		$result_page = $db->query($sql);
		
		$pagesize = 20; //每頁筆數
		
		$numrows = mysqli_num_rows($result_page); //總筆數
		$totalpages = ceil($numrows / $pagesize); //頁數、最後一頁頁碼
		
		
		if(!isset($dataobject)){
		$currentpage = 1 ;
		}else{
		$currentpage = $dataobject ;
		}
		$startnum = ($currentpage -1) * $pagesize;   //開始的筆數
		$startnum = $startnum < 0 ? 0 : $startnum;
		
		$query = "select * from product limit $startnum,$pagesize";
		$result = $db->query($query);
		while ($obj = mysqli_fetch_object($result)) {
		$id = $obj->id ;
		$prod_name = $obj->name ;
		$prod_amount = $obj->pd_count ;
		$prod_price1 = $obj->price_1 ;
		$prod_price2 = $obj->price_2 ;
		
		$stage_query = "select * from stage" ;
		$stage_result = $db->query($stage_query);
		$stage_obj = mysqli_fetch_object($stage_result) ;
		$rate_1 = $stage_obj->three_rate ;
		$rate_2 = $stage_obj->six_rate ;
		$rate_3 = $stage_obj->twelve_rate ;
		$rate_4 = $stage_obj->tf_rate ;
		
		$pric_rate3 = floor($prod_price2 * (($rate_1+100)/100)) ;
		$pric_rate6 = floor($prod_price2 * (($rate_2+100)/100)) ;
		$pric_rate12 = floor($prod_price2 * (($rate_3+100)/100)) ;
		$pric_rate24 = floor($prod_price2 * (($rate_4+100)/100)) ;
		
        $data_array[] = array("id"=>$id,"name"=>$prod_name,"amount"=>$prod_amount,"price1"=>$prod_price1,"price2"=>$prod_price2,"pric_rate3"=>$pric_rate3,"pric_rate6"=>$pric_rate6,"pric_rate12"=>$pric_rate12,"pric_rate24"=>$pric_rate24) ;
    	}
		$db->close();
		return $data_array;		
	}
	
	public function dataCount(){
		$db = new mysqli(dbhost,dbusername,dbpassword,dbname);
		$db->query("SET NAMES utf8");
		
		$sql = "select * from product order by id " ;
		$result_page = $db->query($sql);
		
		$pagesize = 20; //每頁筆數
		
		$numrows = mysqli_num_rows($result_page); //總筆數
		$totalpages = ceil($numrows / $pagesize); //頁數、最後一頁頁碼
		$page_array = array("total"=>$numrows,"totalpages"=>$totalpages);
		$db->close();
		return $page_array;		
	}
	

	
	public function insertProd($dataobject){
		$prod_name = $dataobject["name"];
		$prod_amount = $dataobject["amount"];
		$prod_price1 = $dataobject["price1"];
		$prod_price2 = $dataobject["price2"];
		
		$db = new mysqli(dbhost,dbusername,dbpassword,dbname);
		$db->query("SET NAMES utf8");
		$query = "select * from product order by id desc limit 1";
		$result = $db->query($query);
		$obj = mysqli_fetch_object($result);
		$num = $obj->id ;
		if(empty($num)){
		$data_num = "PD000001" ;
		}else{
		$data_num = (substr($num, 0,2) . substr((1000000 + substr($num , 2,6) + 1) , 1 ,6)) ;
		}
		
		$sql_add = "insert into product set 
					id = '$data_num' , 
					name = '$prod_name' , 
					pd_count = '$prod_amount' , 
					price_1 = '$prod_price1' , 
					price_2 = '$prod_price2' 
					" ;
		$result_add = $db->query($sql_add);
		$db->close();
		return true ;
	}
	
	public function delProd($dataobject){
		$prod_id = $dataobject["id"] ;
		
		$db = new mysqli(dbhost,dbusername,dbpassword,dbname);
		$db->query("SET NAMES utf8");
		$query = "delete from product where id = '$prod_id' limit 1";
		$result = $db->query($query);
		$db->close();
		return true;
	}
	
	public function inqProd($dataobject){
		$prod_id = $dataobject["id"] ;
		
		$db = new mysqli(dbhost,dbusername,dbpassword,dbname);
		$db->query("SET NAMES utf8");
		$query = "select * from product where id = '$prod_id' limit 1";
		$result = $db->query($query);
		$obj = mysqli_fetch_object($result);
		$id = $obj->id ;
		$prod_name = $obj->name ;
		$prod_amount = $obj->pd_count ;
		$prod_price1 = $obj->price_1 ;
		$prod_price2 = $obj->price_2 ;
		$data_array= array("id"=>$id,"name"=>$prod_name,"amount"=>$prod_amount,"price1"=>$prod_price1,"price2"=>$prod_price2) ;
		$db->close();
		return $data_array;
	}

	public function updateProd($dataobject){
		$prod_id = $dataobject["id"] ;
		$prod_name = $dataobject["name"];
		$prod_amount = $dataobject["amount"];
		$prod_price1 = $dataobject["price1"];
		$prod_price2 = $dataobject["price2"];
					
		$db = new mysqli(dbhost,dbusername,dbpassword,dbname);
		$db->query("SET NAMES utf8");
		$query = "update product set 
					name = '$prod_name' , 
					pd_count = '$prod_amount' , 
					price_1 = '$prod_price1' , 
					price_2 = '$prod_price2' 
					where id = '$prod_id' limit 1 
					" ;
		$result = $db->query($query);
		$db->close();
		return true; 					
	}
	
	public function getRateData(){
		$db = new mysqli(dbhost,dbusername,dbpassword,dbname);
		$db->query("SET NAMES utf8");
		
		$sql = "select * from stage" ;
		$result = $db->query($sql);
		$obj = mysqli_fetch_object($result);		 
		$rate_1 = $obj->three_rate ;
		$rate_2 = $obj->six_rate ;
		$rate_3 = $obj->twelve_rate ;
		$rate_4 = $obj->tf_rate ;
		
		$data_array = array("rate1"=>$rate_1,"rate2"=>$rate_2,"rate3"=>$rate_3,"rate4"=>$rate_4);
		$db->close();
		return $data_array;
		
	}
	
	
	public function updateRate($dataObject){
		$db = new mysqli(dbhost,dbusername,dbpassword,dbname);
		$db->query("SET NAMES utf8");
		
		$rate_1 = $dataObject["rate1"];
		$rate_2 = $dataObject["rate2"];
		$rate_3 = $dataObject["rate3"];
		$rate_4 = $dataObject["rate4"];
		
		$sql = "update stage set
				three_rate = '$rate_1',
				six_rate = '$rate_2',
				twelve_rate = '$rate_3',
				tf_rate = '$rate_4' 
				where id = '1' ;
				";
		$result = $db->query($sql);
		$db->close();
		return true;
	}
	
	public function userLogin($dataObject){
		$db = new mysqli(dbhost,dbusername,dbpassword,dbname);
		$db->query("SET NAMES utf8");
		session_start();
		
		$username = $dataObject["username"];
		$password = $dataObject["pwd"];
		
		$sql = "select * from kart_manage where user_account = '$username' and user_password = '$password' and user_valid = 'Y' ";
		$result = $db->query($sql);
		$row = mysqli_fetch_object($result);
		$_SESSION['auth'] = $row->user_auth ;

		$db->close();
		if($_SESSION['auth']){
			return $_SESSION['auth'];	
		}else{
			return false ;
		}
		//$data_array = array("auth"=>$auth);
		
		//return $data_array;
	}
	
	public function checkSession(){
		session_start();
		if($_SESSION['auth']){
			return $_SESSION['auth'];	
		}else{
			return false ;
		}
		
	}
	
	
	
	
	
	
	
}
?>